package com.chuji.crmcj.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.chuji.crmcj.R;
import com.chuji.crmcj.act.ClientDetailAct;
import com.chuji.crmcj.bean.ClientLoseInfo;
import com.chuji.crmcj.utils.NumberUtils;
import com.chuji.crmcj.utils.SPUtils;
import com.chuji.crmcj.utils.UIUtils;
import com.chuji.crmcj.utils.UrlUtils;
import com.chuji.crmcj.utils.VolleyUtil;

import java.util.ArrayList;
import java.util.List;

public class ClientLoseFragment extends Fragment {
    //客户战报列表
    private ListView mLv_client_lose;
    /*自定义适配器*/
    private CustomClientLoseAdapter customClientLoseAdapter;
    /*数据存储*/
    List<ClientLoseInfo.ApiParamObjEntity> loseApproveData;
    /*网络数据*/
    private ClientLoseInfo clientLoseInfo;
    //销售员ID
    String SalesID;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_client_lose, container, false);

        mLv_client_lose = (ListView) view.findViewById(R.id.lv_client_lose);
        initData();
//        //初始化适配器
//        customClientLoseAdapter=new CustomClientLoseAdapter();
//        //数据

//        //填充
//        mLv_client_lose.setAdapter(customClientLoseAdapter);

        return view;
    }

    public void initData() {
        /**
         * 获得评价记录
         */
        getAppraiseData();
    }

    private void getAppraiseData() {
        //第一次
        if (loseApproveData == null) {
            loseApproveData = new ArrayList<ClientLoseInfo.ApiParamObjEntity>();
        }
        requestData();
    }

    private void requestData() {
        //初始化Volley
        SalesID = SPUtils.getString(UIUtils.getContext(), "UserID", "");
        String strURL = String.format(UrlUtils.GET_SALES_CLIENT_LOSE + "&SalesID=" + SalesID + "&PageSize=" + "10" + "&PageIndex=" + "1");
        StringRequest stringRequest = new StringRequest(Request.Method.GET, strURL, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                clientLoseInfo = JSON.parseObject(response, ClientLoseInfo.class);
                loseApproveData.addAll(clientLoseInfo.getApiParamObj());
                UIUtils.runInMainThread(new Runnable() {
                    @Override
                    public void run() {
                        // 判断adapter是否创建
                        if (customClientLoseAdapter == null) {
                            customClientLoseAdapter = new CustomClientLoseAdapter();
                            mLv_client_lose.setAdapter(customClientLoseAdapter);
                        } else {
                            customClientLoseAdapter.notifyDataSetChanged();
                        }
                        //给ListView设置点击事件
                        mLv_client_lose.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                                //判断是否可以访问
                                if (loseApproveData.get(position).getState() == 1) {
                                    UIUtils.showToastSafe("没有访问权限");
                                } else if (loseApproveData.get(position).getState() == 2) {

                                } else {


                                    UIUtils.startActivity(new Intent(UIUtils.getContext(), ClientDetailAct.class));
                                }
                            }
                        });
                    }
                });
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
                System.out.println("失败" + error);
                UIUtils.showToastSafe("请检查网络");
            }
        });
        // Add the request to the RequestQueue.
        VolleyUtil.getRequestQueue().add(stringRequest);
    }


    /**
     * 自定义适配器
     */
    class CustomClientLoseAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return loseApproveData.size();
        }

        @Override
        public Object getItem(int position) {
            return loseApproveData.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup viewGroup) {
            /****1)根据list_item.xml--》得到当前子项元素的布局   【界面】*******/
            //b)声明ViewHolder
            ViewHolder viewHolder = null;
            //c)判断convertView是否为null
            if (convertView == null) {//第一次调用getView（）方法时为 null,意思就是在内存中没有
                viewHolder = new ViewHolder();
                convertView = LayoutInflater.from(UIUtils.getContext()).inflate(R.layout.listview_client_lose, null);
                //初始化组件
                viewHolder.tv_client_name = (TextView) convertView.findViewById(R.id.tv_client_name);
                viewHolder.tv_client_phoneNumber = (TextView) convertView.findViewById(R.id.tv_client_phoneNumber);
                viewHolder.tv_car_type = (TextView) convertView.findViewById(R.id.tv_car_type);
                viewHolder.tv_handle_time = (TextView) convertView.findViewById(R.id.tv_handle_time);
                viewHolder.iv_apply_tag = (ImageView) convertView.findViewById(R.id.iv_client_tag_approve);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            viewHolder.tv_client_name.setText(loseApproveData.get(position).getName());
            if (loseApproveData.size() == 11) {
                viewHolder.tv_client_phoneNumber.setText(NumberUtils.parsePhoneNumber(loseApproveData.get(position).getTel()));
            } else {
                viewHolder.tv_client_phoneNumber.setText(loseApproveData.get(position).getTel());
            }
            viewHolder.tv_car_type.setText(loseApproveData.get(position).getCarModel());
            viewHolder.tv_handle_time.setText(loseApproveData.get(position).getCreateTime());
            if (loseApproveData.get(position).getState() == 0) {
                viewHolder.iv_apply_tag.setVisibility(View.VISIBLE);
                viewHolder.iv_apply_tag.setImageResource(R.drawable.shenhezhong);
            } else if (loseApproveData.get(position).getState() == 1) {
                viewHolder.iv_apply_tag.setVisibility(View.VISIBLE);
                viewHolder.iv_apply_tag.setImageResource(R.drawable.yiqueren);
            } else if (loseApproveData.get(position).getState() == 2) {
                viewHolder.iv_apply_tag.setVisibility(View.VISIBLE);
                viewHolder.iv_apply_tag.setImageResource(R.drawable.butongguo);
            } else {
                viewHolder.iv_apply_tag.setVisibility(View.GONE);
            }
            return convertView;
        }
    }

    static class ViewHolder {
        private TextView tv_client_name;
        private TextView tv_client_phoneNumber;
        private TextView tv_car_type;
        private TextView tv_handle_time;
        private ImageView iv_apply_tag;
    }

    @Override
    public void onPause() {
        super.onPause();
    }
}
